package com.shanghai.market.plshnaghai.home.measurement;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.shanghai.market.plshnaghai.AppApplication;
import com.shanghai.market.plshnaghai.R;
import com.shanghai.market.plshnaghai.abstracts.AbstractModelGet;
import com.shanghai.market.plshnaghai.abstracts.AbstractModelPost;
import com.shanghai.market.plshnaghai.entity.AppData;
import com.shanghai.market.plshnaghai.entity.home.measurement.Quizzes;
import com.shanghai.market.plshnaghai.interfaces.IListView;
import com.shanghai.market.plshnaghai.interfaces.IObjectView;
import com.shanghai.market.plshnaghai.model.measurement.QuizzesModelPost;
import com.shanghai.market.plshnaghai.model.measurement.QuizzesModelget;
import com.shanghai.market.plshnaghai.net.VolleyParams;
import com.shanghai.market.plshnaghai.publics.BaseActivity;
import com.shanghai.market.plshnaghai.publics.LogCat;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试考题页面
 */
public class QuizzesActivity extends BaseActivity implements IListView<Quizzes>, IObjectView<Object>, View.OnClickListener {

    private AbstractModelGet modelGet;
    private AbstractModelPost modelPost;
    private List<Quizzes> data;
    private ListView mList;
    private QuizzesAdapter mAdapter;
    private TextView mTextConfirm;
    int index = 0;

    private TextView titleName;
    private TextView a;
    private TextView b;
    private TextView c;
    private TextView mTextViewcount;
    private List<String> v_type = new ArrayList<>();
    private List<String> a_type = new ArrayList<>();
    private List<String> k_type = new ArrayList<>();
    private Quizzes quizzes;

    @Override
    public void initParms() {

    }

    @Override
    public View bindView() {
        View view = LayoutInflater.from(this).inflate(R.layout.activity_quizzes, null);
        return view;
    }

    @Override
    public int bindLayout() {
        return 0;
    }

    @Override
    public void initView(View view) {
//        mList = (ListView) view.findViewById(R.id.listView);
        mTextConfirm = (TextView) view.findViewById(R.id.textview_confirm);
        titleName = (TextView) view.findViewById(R.id.title_name);
        a = (TextView) view.findViewById(R.id.a);
        b = (TextView) view.findViewById(R.id.b);
        c = (TextView) view.findViewById(R.id.c);
        mTextViewcount = (TextView) view.findViewById(R.id.count);
    }

    @Override
    public void setListener() {
        a.setOnClickListener(this);
        b.setOnClickListener(this);
        c.setOnClickListener(this);
        mTextConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                a.setBackgroundResource(R.color.white);
                b.setBackgroundResource(R.color.white);
                c.setBackgroundResource(R.color.white);

                if (index < data.size()) {
                    if (data.size() == (index + 1)) {
                        mTextConfirm.setText("提交考卷");
                    }
                    addData();
                    add(data);
                } else {
                    addData();
                    showToast("上传数据");
                    //把值分类
                    for (int i = 0; i < list.size(); i++) {
                        if ("V".equals(list.get(i))) {
                            v_type.add(list.get(i));
                        } else if ("A".equals(list.get(i))) {
                            a_type.add(list.get(i));
                        } else if ("K".equals(list.get(i))) {
                            k_type.add(list.get(i));
                        }
                    }
//                    LogCat.i("atype " + a_type.size());
//                    LogCat.i("vtype" + v_type.size());
//                    LogCat.i("ktype" + k_type.size());
                    AppApplication app = AppApplication.getInstance();
                    AppData data = app.getAppData();
                    VolleyParams params = new VolleyParams();
                    params.put("uid", data.getUserId() + "");
                    params.put("v_num", v_type.size() + "");
                    params.put("a_num", a_type.size() + "");
                    params.put("k_num", k_type.size() + "");
                    modelPost.setParams(params);
                    modelPost.getData();
                }
            }
        });

    }

    List<String> list = new ArrayList<>();

    //获取值
    private void addData() {
        if (aa == true) {
            list.add(quizzes.getA_type());
        }
        if (bb == true) {
            list.add(quizzes.getB_type());
        }
        if (cc == true) {
            list.add(quizzes.getC_type());
        }
//        LogCat.i("aaaa " + quizzes.getA_type());
//        LogCat.i("bbbb" + quizzes.getB_type());
//        LogCat.i("cccc" + quizzes.getC_type());
//        for (int i = 0; i < list.size(); i++) {
//            LogCat.i("list  " + list.get(i));
//        }
    }

    @Override
    public void doBusiness(Context mContext) {
        mBarTitle.setText("NLP测试题");
        modelGet = new QuizzesModelget(QuizzesActivity.this);
        modelPost = new QuizzesModelPost(QuizzesActivity.this);
        modelGet.getData();
    }

    @Override
    protected void onStop() {
        super.onStop();
        index = 0;
    }

    @Override
    protected void onResume() {
        super.onResume();
        index = 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        index = 0;
    }

    @Override
    public void failure(String message) {

    }


    @Override
    public void success(Object o) {

    }

    @Override
    public void success(List<Quizzes> list) {
        data = list;
        add(list);
    }


    private void add(List<Quizzes> list) {
        int size = list.size();
        data = list;
        LogCat.i("size   " + size);
        if (size > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i == index) {
                    index++;
                    quizzes = list.get(i);
                    titleName.setText(quizzes.getId() + '.' + quizzes.getTopic());
                    a.setText("A." + quizzes.getA());
                    b.setText("B." + quizzes.getB());
                    c.setText("C." + quizzes.getC());
//                    LogCat.i("index    " + index);
                    mTextViewcount.setText("完成度：" + "(" + index + "/" + size + ")");
                    return;
                }
            }
        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.a:
                equalsType("a");
                break;
            case R.id.b:
                equalsType("b");
                break;
            case R.id.c:
                equalsType("c");
                break;
        }
    }

    boolean aa = false;
    boolean bb = false;
    boolean cc = false;

    private void equalsType(String str) {
        if (str.equals("a")) {
            a.setBackgroundResource(R.color.main_warning_text_color);
            b.setBackgroundResource(R.color.white);
            c.setBackgroundResource(R.color.white);
            aa = true;
            bb = false;
            cc = false;
        } else if (str.equals("b")) {
            b.setBackgroundResource(R.color.main_warning_text_color);
            a.setBackgroundResource(R.color.white);
            c.setBackgroundResource(R.color.white);
            aa = false;
            bb = true;
            cc = false;
        } else if (str.equals("c")) {
            c.setBackgroundResource(R.color.main_warning_text_color);
            a.setBackgroundResource(R.color.white);
            b.setBackgroundResource(R.color.white);
            aa = false;
            bb = false;
            cc = true;
        }

    }
}
